package com.jy.appmarket.screenlock.view;

import java.util.ArrayList;
import java.util.Collections;

import com.jy.appmarket.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;


public class BladeView extends View {
	private OnItemClickListener mOnItemClickListener;
	private String[] str = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
			"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
			"X", "Y", "Z" };
	private ArrayList<String> b = new ArrayList<String>();
	int choose = -1;
	Paint paint = new Paint();
	boolean showBkg = false;
	private PopupWindow mPopupWindow;
	private TextView mPopupText;
	private int mCharHeight = 15;
	private int mTopIcon = -1;
	private boolean isOnlyAlphabet = false;
	private Bitmap mBitmap;

	public BladeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Collections.addAll(b, str);
		init(context, attrs);
		mCharHeight = context.getResources().getDimensionPixelSize(
				R.dimen.blade_view_text_size);
	}

	public BladeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Collections.addAll(b, str);
		init(context, attrs);
		mCharHeight = context.getResources().getDimensionPixelSize(
				R.dimen.blade_view_text_size);
	}

	public BladeView(Context context) {
		super(context);
		Collections.addAll(b, str);
		mCharHeight = context.getResources().getDimensionPixelSize(
				R.dimen.blade_view_text_size);
	}

	private void init(Context context, AttributeSet attrs) {
		if (attrs != null) {
			TypedArray array = context.obtainStyledAttributes(attrs,
					R.styleable.BladeView);
			mTopIcon = array.getResourceId(R.styleable.BladeView_topIcon,
					mTopIcon);
			isOnlyAlphabet = array.getBoolean(
					R.styleable.BladeView_onlyAlphabet, isOnlyAlphabet);
			array.recycle();
		}
		if (!isOnlyAlphabet) {
			b.add(0, "#");
		}
		if (mTopIcon != -1) {
			mBitmap = drawableToBitamp(getResources().getDrawable(mTopIcon));
			b.add(0, "0");
		}

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (showBkg) {
			canvas.drawColor(Color.parseColor("#4d000000"));
		} else {
			canvas.drawColor(Color.parseColor("#00000000"));
		}

		int height = getHeight();
		int width = getWidth();
		int singleHeight = height / b.size();
		for (int i = 0; i < b.size(); i++) {
			// 右边的字体颜色
			paint.setColor(Color.parseColor("#dddddd"));
			// paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setTextSize(mCharHeight);
			paint.setFakeBoldText(true);
			paint.setAntiAlias(true);
			if (i == choose) {
				paint.setColor(Color.parseColor("#dddddd"));
			}
			float xPos = width / 2 - paint.measureText(b.get(i)) / 2;
			float yPos = singleHeight * i + singleHeight;
			if (mTopIcon != -1) {
				float xPoss = width / 2 - mBitmap.getWidth() / 2;
				float yPoss = mBitmap.getHeight();
				if (i == 0) {
					canvas.drawBitmap(mBitmap, xPoss, yPoss, paint);
				} else {
					canvas.drawText(b.get(i), xPos, yPos, paint);
				}
			} else {
				canvas.drawText(b.get(i), xPos, yPos, paint);
			}
			paint.reset();
		}

	}

	private Bitmap drawableToBitamp(Drawable drawable) {
		BitmapDrawable bd = (BitmapDrawable) drawable;
		return bd.getBitmap();
	}

	@Override
	protected Parcelable onSaveInstanceState() {
		dismissPopup();
		return super.onSaveInstanceState();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = choose;
		final int c = (int) (y / getHeight() * b.size());

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			showBkg = true;
			if (oldChoose != c) {
				if (c >= 0 && c < b.size()) {
					performItemClicked(c);
					choose = c;
					invalidate();
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c) {
				if (c >= 0 && c < b.size()) {
					performItemClicked(c);
					choose = c;
					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			showBkg = false;
			choose = -1;
			dismissPopup();
			invalidate();
			break;
		}
		return true;
	}

	private void showPopup(int item) {
		if (mPopupWindow == null) {
			mPopupText = new TextView(getContext());
			// mPopupText
			// .setBackgroundResource(R.drawable.ic_contacts_index_backgroud_sprd);
			mPopupText.setBackgroundColor(Color.TRANSPARENT);
			mPopupText.setTextColor(Color.parseColor("#0078ff"));
			mPopupText.setTextSize(30);
			mPopupText.setGravity(Gravity.CENTER_HORIZONTAL
					| Gravity.CENTER_VERTICAL);
			mPopupWindow = new PopupWindow(mPopupText,
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		}

		String text = "";
		// if(item == 0){
		// text = "ss";
		// }
		if (!isOnlyAlphabet) {
			if (mTopIcon != -1) {
				if (item == 1) {
					text = "#";
				} else {
					text = Character.toString((char) ('A' + item - 2));
				}
			} else {
				if (item == 0) {
					text = "#";
				} else {
					text = Character.toString((char) ('A' + item - 1));
				}
			}
		} else {
			text = Character.toString((char) ('A' + item));
		}
		mPopupText.setText(text);
		if (mPopupWindow.isShowing()) {
			mPopupWindow.update();
		} else {
			mPopupWindow.showAtLocation(getRootView(),
					Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
		}
	}

	private void dismissPopup() {
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		mOnItemClickListener = listener;
	}

	private void performItemClicked(int item) {
		if (mOnItemClickListener != null) {
			mOnItemClickListener.onItemClick(b.get(item));
			if (mTopIcon != -1) {
				if (item != 0) {
					showPopup(item);
				}else{
					dismissPopup();
				}
			} else {
				showPopup(item);
			}
		}
	}

	public interface OnItemClickListener {
		void onItemClick(String s);
	}

}
